import Vue from 'vue'
import VueRouter from 'vue-router'

const MainView = () => import(/* webpackChunkName: "MainView" */ '../views/mainView/MainView.vue')
const home = () => import(/* webpackChunkName: "home" */ '../views/mainView/home.vue')
const new_goods = () => import(/* webpackChunkName: "new_goods" */ '../views/mainView/new_goods.vue')
const all_goods = () => import(/* webpackChunkName: "all_goods" */ '../views/mainView/all_goods.vue')
const login_register = () => import(/* webpackChunkName: "login_register" */ '../views/login_register.vue')

const admin = () => import(/* webpackChunkName: "admin" */ '../views/admin/admin.vue')
const chart = () => import(/* webpackChunkName: "chart" */ '../views/admin/chart.vue')
const user_manage = () => import(/* webpackChunkName: "user_manage" */ '../views/admin/user_manage.vue')
const goods_manage = () => import(/* webpackChunkName: "goods_manage" */ '../views/admin/goods_manage.vue')
const warehouse = () => import(/* webpackChunkName: "warehouse" */ '../views/admin/warehouse.vue')
const warehouse_detail = () => import(/* webpackChunkName: "warehouse_detail" */ '../views/admin/warehouse_detail.vue')
const inbound_manage = () => import(/* webpackChunkName: "inbound_manage" */ '../views/admin/inbound_manage.vue')
const inbound_order_detail = () => import(/* webpackChunkName: "inbound_order_detail" */ '../views/admin/inbound_order_detail.vue')
const outbound_manage = () => import(/* webpackChunkName: "outbound_manage" */ '../views/admin/outbound_manage.vue')
const outbound_order_detail = () => import(/* webpackChunkName: "outbound_order_detail" */ '../views/admin/outbound_order_detail.vue')
const change_password = () => import(/* webpackChunkName: "change_password" */ '../views/admin/change_password.vue')
const personal_information = () => import(/* webpackChunkName: "personal_information" */ '../views/admin/personal_information.vue')

const list = () => import(/* webpackChunkName: "list" */ '../components/list.vue')
const place = () => import(/* webpackChunkName: "place" */ '../components/place.vue')
const index = () => import(/* webpackChunkName: "index" */ '../views/dashboard/index.vue')

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/login_register'
    },
    {
      path: '/login_register',
      component: login_register,
      meta: {
        title: '欢迎棉工坊'
      }
    },
    {
      path: '/MainView',
      component: MainView,
      redirect: '/home',
      meta: {
        title: '棉工坊'
      },
      children: [
        { path: '/home', component: home },
        { path: '/new_goods', component: new_goods },
        { path: '/all_goods', component: all_goods },
      ]
    },
    {
      path: '/admin',
      component: admin,
      redirect: '/chart',
      meta: {
        title: '棉工坊管理界面'
      },
      children: [
        { path: '/chart', component: chart },
        { path: '/user_manage', component: user_manage, meta: { title: '棉工坊管理界面' }, },
        { path: '/goods_manage', component: goods_manage },
        { path: '/warehouse', component: warehouse },
        { path: '/warehouse_detail', component: warehouse_detail },
        { path: '/inbound_manage', component: inbound_manage },
        { path: '/inbound_order_detail', name: 'inbound_order_detail', component: inbound_order_detail },
        { path: '/outbound_manage', component: outbound_manage },
        { path: '/outbound_order_detail', name: 'outbound_order_detail', component: outbound_order_detail },
        { path: '/change_password', component: change_password },
        { path: '/personal_information', component: personal_information },
      ]
    },
    {
      path: '/list',
      component: list,
    },
    {
      path: '/place',
      component: place,
    },
  ]
})

const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

// const originalPush = VueRouter.prototype.router.push
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this.location).catch(err => err)
// }

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to 将要访问的路径
  //from 代表从哪个路径跳转而来
  //next 是一个函数，表示放行
  //next() 放行
  //next('/login_register') 强制跳转
  to.meta.title && (document.title = to.meta.title);
  next()

  if (to.path === '/login_register') return next()
  //获取token
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) return next('/login_register');
  next();
});

export default router
